﻿using System.Reflection;
using Microsoft.Extensions.DependencyInjection;
using Quartz.Impl;
using Quartz.Spi;
using Quartz;
using SBlogCore.TaskJob.HostServices;
using SBlogCore.TaskJob.Models;

namespace SBlogCore.TaskJob
{
    public  static class TaskJobSetUp
    {
        public static IServiceCollection AddTaskJobSetUp(this IServiceCollection services, Assembly[] assemblies)
        {
            //services.AddSingleton<IJobFactory, SBlogCoreJobFactory>();
            services.AddSingleton<ISchedulerFactory, StdSchedulerFactory>();
            services.AddHostedService<QuartzService>();

           var types= assemblies.SelectMany(w => w.GetTypes()).Where(w=>w.IsAssignableTo(typeof(IJob))&&w.Name!=nameof(IJob));
           foreach (var type in types)
           {
               services.AddSingleton(type);
               //services.AddSingleton(
               //    new SBlogCoreJobSchedule(type, "0/1 * * * * ? ")
               //);
            }
            return services;
        }
    }
}
